import QtQuick 2.0
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.0


Item {
    id: serial_select_view
    objectName: "SerialSelect"
    width: parent.width - 80
    height: parent.height
    property var comlist: []
    visible: true
    property bool uart_open: false
    signal notify(string message)

    Component.onCompleted: {
        //信号连接slot  signal.connect(id.slot)
        console.log(parent)
        notify.connect(root.on_notify)
    }

    ColorAnimation {
        from: "white"
        to: "black"
        duration: 200
    }
    Rectangle{
        x: 0
        y: 0
        width: parent.width
        height: parent.height
        color: "#aeaeae"
        scale: 1
        transformOrigin: Item.Center

        GridLayout {
            x: 50
            y: 41
            rows: 6
            columns: 2

            Label {
                id: label
                text: qsTr("串口号: ")
                verticalAlignment: Text.AlignVCenter
                Layout.preferredHeight: 40
                Layout.preferredWidth: 125
                font.pointSize: 20
                wrapMode: Text.NoWrap
                renderType: Text.QtRendering
                horizontalAlignment: Text.AlignHCenter
            }
            ComboBox {
                id: comPort
                width: 200
                model: comlist
            }
            Label {
                id: label1
                text: qsTr("波特率: ")
                verticalAlignment: Text.AlignVCenter
                Layout.preferredHeight: 40
                Layout.preferredWidth: 125
                horizontalAlignment: Text.AlignHCenter
                font.pointSize: 20
                renderType: Text.QtRendering
                wrapMode: Text.NoWrap
            }

            ComboBox {
                id: baurate
                width: 200
                model:[
                    "300",
                    "600",
                    "1200",
                    "2400",
                    "4800",
                    "9600",
                    "14400",
                    "19200",
                    "38400",
                    "56000",
                    "57600",
                    "115200",
                    "128000",
                    "256000",
                ]
                currentIndex: 6
            }

            Label {
                id: label2
                text: qsTr("数据位: ")
                verticalAlignment: Text.AlignVCenter
                Layout.preferredHeight: 40
                Layout.preferredWidth: 125
                horizontalAlignment: Text.AlignHCenter
                font.pointSize: 20
                renderType: Text.QtRendering
                wrapMode: Text.NoWrap
            }

            ComboBox {
                id: dataBits
                width: 200
                currentIndex:  3

                model: [
                    "5",
                    "6",
                    "7",
                    "8",
                ]

            }

            Label {
                id: label3
                text: qsTr("停止位: ")
                verticalAlignment: Text.AlignVCenter
                Layout.preferredHeight: 40
                Layout.preferredWidth: 125
                horizontalAlignment: Text.AlignHCenter
                font.pointSize: 20
                renderType: Text.QtRendering
                wrapMode: Text.NoWrap
            }

            ComboBox {
                id: stopBits
                width: 200

                model: [
                    "1",
                    "1.5",
                    "2",
                ]
            }

            Label {
                id: label4
                text: qsTr("校验位: ")
                verticalAlignment: Text.AlignVCenter
                Layout.preferredHeight: 40
                Layout.preferredWidth: 125
                horizontalAlignment: Text.AlignHCenter
                font.pointSize: 20
                renderType: Text.QtRendering
                wrapMode: Text.NoWrap
            }

            ComboBox {
                id: verify
                width: 200

                model: [
                    "none",
                    "odd",
                    "even",
                    "mark",
                    "space"
                ]
            }

            Label {
                id: label5
                text: qsTr("流控: ")
                verticalAlignment: Text.AlignVCenter
                Layout.preferredHeight: 40
                Layout.preferredWidth: 125
                horizontalAlignment: Text.AlignHCenter
                font.pointSize: 20
                renderType: Text.QtRendering
                wrapMode: Text.NoWrap
            }

            ComboBox {
                id: flow
                width: 200
                model: [
                    "none",
                    "software",
                    "hardware",
                ]
            }
        }

        Button {
            id: button
            x: 97
            y: 339
            text: qsTr("打开串口")
            onClicked: {
                if(!uart_open){
                    let ret = DataWrap.openUart(comPort.currentText,
                                      baurate.currentText,
                                      dataBits.currentText,
                                      stopBits.currentText,
                                      flow.currentText)
                    if(ret === 0){
                        uart_open = true
                        button.text = "关闭串口"
                        serial_select_view.notify("串口已经连接")
                    }
                }else{
                    let ret = DataWrap.closeSerial();
                    if(ret === 0){
                        button.text = "打开串口"
                        uart_open = false
                        serial_select_view.notify("串口已经关闭")
                    }
                }
            }
        }
    }
}
